using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BookSpace;
using BookBarter;

public partial class BookProfile : System.Web.UI.Page
{
    /// <summary>
    /// publish this \\ist411svr\bookbarter
    /// find it at: ist411svr.ist.psu.edu
    /// </summary>
  
    BookBarter.Person currentPerson;
    BookSpace.Book currentBook;
    BookBarter.Book currentBook2;

    //this is the url format for searching by author
    //Response.Redirect("~/SearchResults.aspx?txtAuthor=dfads");

    protected void Page_Load(object sender, EventArgs e)
    {

        //create a book for debugging purposes
        try
        {
            BookSpace.Book testBook = new BookSpace.Book("Usability Engineering", "Jakob Nielson", "978-0125184069",
                362, "English", "Morgan Kauffman", "Book about usability engineering", "Software/Design",
                "http://ec1.images-amazon.com/images/P/0125184069.01._BO2,204,203,200_PIsitb-dp-500-arrow,TopRight,45,-64_OU01_SCLZZZZZZZ_V45565115_AA240_SH20_.jpg",
                "gadfz");
        }
        catch (Exception x)
        {
        }

        BookInfoPanel.Visible = true;
        AuthUserPanel.Visible = true;
        RemoteAdminPanel.Visible = true;
 
        try
        {
            currentPerson = BookBarter.Person.GetPerson(User.Identity.Name);
        }
        catch(Exception error){}


        //AuthUser, RemoteAdmin
        if(User.IsInRole("AuthUser") == true)
        {
            //unhide panels
            AuthUserPanel.Visible = true;
        }
        if (User.IsInRole("RemoteAdmin") == true)
        {
            //unhide panels
            AuthUserPanel.Visible = true;
            RemoteAdminPanel.Visible = true;
        }

        //call the GetBook method to retrieve the book information
        try
        {
            
            //this uses my Bookspace.Book class
            currentBook = BookSpace.Book.GetBook(Request.QueryString[0]);
            
            //set all the corresponding text on the page
            BookImage.ImageUrl = currentBook.Url;
            Title.Text = currentBook.Title;
            Author.Text = currentBook.Author;
            ISBN.Text = currentBook.Isbn;
            Pages.Text = currentBook.Pages.ToString();
            Language.Text = currentBook.Language;
            Publisher.Text = currentBook.Publisher;
            //Category.Text = currentBook.Category;
            Description.Text = currentBook.Description;


            /*
            //this uses the Integration.Book class
            Guid _guid = new Guid(Request.QueryString[0]);
            currentBook2 = BookBarter.Book.GetBook(_guid);
            //set all the corresponding text on the page
            BookImage.ImageUrl = currentBook2.ImageUrl;
            Title.Text = currentBook2.Title;
            Author.Text = currentBook2.Author;
            ISBN.Text = currentBook2.ISBN;
            Pages.Text = currentBook2.NumPages.ToString();
            Language.Text = currentBook2.Language;
            Publisher.Text = currentBook2.Publisher;
            Description.Text = currentBook2.Description;
            */

            Page.Header.Title = currentBook.Title + ": " + currentBook.Author;

        }
        catch (BookBarter.NoSuchBook bookError)
        {
            //change this to something useful
            Description.Text = bookError.Message;
        }
        catch (FormatException guid_error)
        {
            //change this to something useful
            Language.Text = guid_error.Message;
        }
        catch (InvalidOperationException connString)
        {
            //change this to something useful
            ISBN.Text = connString.Message;
        }
      
    }
    protected void AddWantsButton_Click(object sender, EventArgs e)
    {
        //createBookWant?
        ConditionDropDown.Visible = true;
        ConditionDropDown.Enabled = true;

        ConditionDropDownValidator.Visible = true;
        ConditionDropDownValidator.Enabled = true;

        //BookBarter.BookWant.CreateBookWant(currentPerson, currentBook, 
        if (ConditionDropDown.Text.Equals("New")) { BookBarter.BookWant.CreateBookWant(currentPerson, currentBook2, Condition.New); }
        else if (ConditionDropDown.Text.Equals("Excellent")) { BookBarter.BookWant.CreateBookWant(currentPerson, currentBook2, Condition.Excellent); }
        else if (ConditionDropDown.Text.Equals("VeryGood")) { BookBarter.BookWant.CreateBookWant(currentPerson, currentBook2, Condition.VeryGood); }
        else if (ConditionDropDown.Text.Equals("Fair")) { BookBarter.BookWant.CreateBookWant(currentPerson, currentBook2, Condition.Fair); }
        else if (ConditionDropDown.Text.Equals("Poor")) { BookBarter.BookWant.CreateBookWant(currentPerson, currentBook2, Condition.Poor); }
        else { ConditionDropDownValidator.Text = "Select a condition..."; }
    }
    protected void AddHavesButton_Click(object sender, EventArgs e)
    {
        //createBookHave?
        //Condition.New, Condition.Excellent, Condition.VeryGood, Condition.Fair, Condition.Poor
        //new, excellent, verygood, fair, poor

        
        //BookBarter.BookHave.CreateBookHave(currentPerson, currentBook, 
        if (ConditionDropDown.Text.Equals("New")) { BookBarter.BookHave.CreateBookHave(currentPerson, currentBook2, Condition.New); }
        else if (ConditionDropDown.Text.Equals("Excellent")) { BookBarter.BookHave.CreateBookHave(currentPerson, currentBook2, Condition.Excellent); }
        else if (ConditionDropDown.Text.Equals("VeryGood")) { BookBarter.BookHave.CreateBookHave(currentPerson, currentBook2, Condition.VeryGood); }
        else if (ConditionDropDown.Text.Equals("Fair")) { BookBarter.BookHave.CreateBookHave(currentPerson, currentBook2, Condition.Fair); }
        else if (ConditionDropDown.Text.Equals("Poor")) { BookBarter.BookHave.CreateBookHave(currentPerson, currentBook2, Condition.Poor); }
        else { ConditionDropDownValidator.Text = "Select a condition..."; }
        
    }
    protected void RmWantsButton_Click(object sender, EventArgs e)
    {
        //deleteBookWant?
    }
    protected void RmHavesButton_Click(object sender, EventArgs e)
    {
        //deleteBookHave?
    }
    protected void EditBookButton_Click(object sender, EventArgs e)
    {
        //editBook?

        Title.Visible = false;
        Author.Visible = false;
        ISBN.Visible = false;
        Pages.Visible = false;
        Language.Visible = false;
        Publisher.Visible = false;
        Description.Visible = false;

        TitleTextBox.Visible = true;
        TitleTextBox.Text = Title.Text;
        AuthorTextBox.Visible = true;
        AuthorTextBox.Text = Author.Text;
        ISBNTextBox.Visible = true;
        ISBNTextBox.Text = ISBN.Text;
        PagesTextBox.Visible = true;
        PagesTextBox.Text = Pages.Text;
        LanguageTextBox.Visible = true;
        LanguageTextBox.Text = Language.Text;
        PublisherTextBox.Visible = true;
        PublisherTextBox.Text = Publisher.Text;
        DescriptionTextBox.Visible = true;
        DescriptionTextBox.Text = Description.Text;

        TitleTextBox.Focus();
        

    }
}
